package com.blueheart.music.controller;

import com.blueheart.music.common.ResultUtil;
import com.blueheart.music.consts.MyConst;
import com.blueheart.music.exception.BaseExceptionEnum;
import com.blueheart.music.model.pojo.Collect;
import com.blueheart.music.service.CollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * @author gali
 * @date 2022/4/10 17:20
 */
@RestController
public class CollectController {
    @Autowired
    private CollectService collectService;

    /**
     * 添加收藏
     * @param request
     * @return
     */
    @PostMapping("/collect/addCollect")
    public ResultUtil addCollect(HttpServletRequest request){
        String userId = request.getParameter(MyConst.USER_ID).trim();
        String type = request.getParameter(MyConst.TYPE).trim();
        if (StringUtils.isEmpty(userId)||StringUtils.isEmpty(type)){
            return  ResultUtil.error(BaseExceptionEnum.PARAM_ERROR);
        }
        Collect collect = new Collect();
        collect.setUserId(new Integer(userId));
        collect.setType(new Integer(type));
        if (new Integer(type)==0){  //歌曲
            String songId = request.getParameter(MyConst.SONGID).trim();
            collect.setSongId(new Integer(songId));
            collect.setSongListId(null);
        }else {
            String songListId = request.getParameter(MyConst.SONGLISTID).trim();
            collect.setSongId(null);
            collect.setSongListId(new Integer(songListId));
        }
        Date date = new Date();
        collect.setCreateTime(new Timestamp(date.getTime()));
        boolean flag = collectService.addCollect(collect);
        if (flag){
            return ResultUtil.success();
        }
        return ResultUtil.error(BaseExceptionEnum.ADD_ERROR);
    }

    /**
     * 删除收藏
     * @param id
     * @return
     */
    @PostMapping("/collect/deleteCollect")
    public ResultUtil deleteCollect(@RequestParam("id") Integer id){
        if (id>0){
            boolean flag = collectService.removeCollect(id);
            if (flag){
                return ResultUtil.success();
            }
            return ResultUtil.error(BaseExceptionEnum.REMOVE_ERROR);
        }
        return ResultUtil.error(BaseExceptionEnum.PARAM_ERROR);
    }

    /**
     * 查询全部收藏
     * @return
     */
    @GetMapping("/collect/findAll")
    public ResultUtil findAll(){
        List<Collect> all = collectService.findAll();
        if (all.size()>0){
            return ResultUtil.success(all);
        }
        return ResultUtil.error(BaseExceptionEnum.SELETE_ERROR);
    }

    /**
     * 根据id查询收藏
     * @param id
     * @return
     */
    @PostMapping("/collect/findById")
    public ResultUtil findById(@RequestParam("id") Integer id){
        if (id>0){
            Collect byId = collectService.findById(id);
            if (byId!=null){
                return ResultUtil.success(byId);
            }
            return ResultUtil.error(BaseExceptionEnum.SELETE_ERROR);
        }
        return ResultUtil.error(BaseExceptionEnum.SELETE_ERROR);
    }

    /**
     * 查询用户个人收藏
     * @param userId
     * @return
     */
    @PostMapping("/collect/findByUserId")
    public ResultUtil findByUserId(@RequestParam("userId") Integer userId){
        if (userId>0){
            List<Collect> byUserId = collectService.findByUserId(userId);
            if (byUserId.size()>0){
                return ResultUtil.success(byUserId);
            }
            return ResultUtil.error(BaseExceptionEnum.SELETE_ERROR);
        }
        return ResultUtil.success(BaseExceptionEnum.PARAM_ERROR);
    }

    /**
     * 检查用户是否收藏了该歌曲
     * @param userId
     * @param songId
     * @return
     */
    @PostMapping("/collect/checkInSong")
    public ResultUtil checkInSong(@RequestParam("userId") Integer userId, @RequestParam("songId") Integer songId) {
        if (userId>0&&songId>0){
            boolean b = collectService.checkSongInUser(userId, songId);
            if (b){
                return ResultUtil.error(BaseExceptionEnum.SONG_EXIST);
            }
            return ResultUtil.success();
        }
        return ResultUtil.success(BaseExceptionEnum.PARAM_ERROR);
    }

    /**
     * 检查用户是否收藏了该歌单
     * @param userId
     * @param songListId
     * @return
     */
    @PostMapping("/collect/checkInSongList")
    public ResultUtil checkInSongList(@RequestParam("userId") Integer userId, @RequestParam("songListId") Integer songListId){
        if (userId>0&&songListId>0){
            boolean b = collectService.checkSongInUser(userId, songListId);
            if (b){
                return ResultUtil.error(BaseExceptionEnum.SONGLIST_EXIST);
            }
            return ResultUtil.success();
        }
        return ResultUtil.success(BaseExceptionEnum.PARAM_ERROR);
    }

    /**
     * 查询主键
     * @param userId
     * @param songId
     * @return
     */
    @PostMapping("/collect/findCollectId")
    public ResultUtil findCollectId(@RequestParam("userId") Integer userId, @RequestParam("songId") Integer songId){
        if (userId>0&&songId>0){
            Collect collectId = collectService.findCollectId(userId, songId);
            if (collectId!=null){
                return ResultUtil.success(collectId);
            }
            return ResultUtil.error(BaseExceptionEnum.SELETE_ERROR);
        }
        return ResultUtil.success(BaseExceptionEnum.PARAM_ERROR);
    }
}
